<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Crypt\Driver\Think;
class GoodsController extends Controller{
    /**
     * list goods
     */
    public function goods_list(){
        $this->display();
    }
    /**
     * add goods
     */
    public function goods_add(){
        if (!IS_POST) {
            $cat=D('Cat');
            $cat_list=$cat->select();
            $cat_tree=$cat->getCatTree($cat_list,$id=0,$lev=2);
            $this->assign('cat_tree',$cat_tree);
            $this->display();
            exit;
        }
        $goods=D('Goods');
        $info=$goods->upload();
        if (!$info){
            $this->error('文件上传失败');
        }
        $image = new \Think\Image();
        $image->open('./Uploads'.$info['ori_img']['savepath'].$info['ori_img']['savename']);
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        $save='./Uploads/Shop/Admin/Images/thumb/'.$info['ori_img']['savename'];
        $thumb=$image->thumb(170, 170,\Think\Image::IMAGE_THUMB_FILLED)->save($save);
        if(!$thumb){
            $this->error('没有生成缩略图');
        }
//        生成水印照片
//        $image->open($save)->water('./Uploads/Shop/Admin/Images/water/')->save(mt_rand(1000,9999).'.jpg');
//        show_bug($save);
        $data=$_POST;
        $data['ori_img']='./Uploads'.$info['ori_img']['savepath'].$info['ori_img']['savename'];
        $data['thumb_img']=$save;
        $add=$goods->add($data);
            if(!$add){
            echo '商品添加失败';
        }
        $this->success('商品添加成功');
    }
    public function goods_edit(){
        $this->display();
    }
    public function goods_trash(){
        $this->display();
    }
    public function goods_type_add(){
        $this->display();
    }
    public function goods_type_edit(){
        $this->display();
    }
    public function goods_type_list(){
        $this->display();
    }
}

